package cn.iot.common.utils;

import cn.iot.common.constant.CommonConstant;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/7 13:13
 * @version：V1.0
 * 时间转换工具类
 */
public class DateTimeUtils {
    /**
     * 格式化时间
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date dateTimeFormat(double time)  {
        long times = new Double(time).longValue();
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(times * 1000);
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= null;
        try {
            date= format.parse(format.format(gc.getTime()));
        }catch (ParseException exception){
            exception.printStackTrace();
        }
        return date;
    }

    /**
     * 获取两个时间之间的间隔
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int compare(Date date1,Date date2) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        String str = simpleDateFormat.format(date2)+" "+ CommonConstant.ATTENDANCE_TIME;
        return  date1.compareTo(simpleDateFormat2.parse(str));
    }

    /**
     * 获取两个日期之间的具体日期(包括最后一天)
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static List<String> getDatesRange(String startDate,String endDate) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        List<String> datesRange=new ArrayList<>();
        Calendar calendar=new GregorianCalendar();
        calendar.setTime(sdf.parse(startDate));
        Calendar endCalendar=new GregorianCalendar();
        endCalendar.setTime(sdf.parse(endDate));
        // 如果开始、结束日期相同，则while循环次数为0
        while (calendar.before(endCalendar)){
            calendar.add(Calendar.DATE,1);
        }
        datesRange.add(endDate);
        return datesRange;
    }

    /**
     * 获取当前时间所在周的周一和周日的日期时间
     * @return
     */
    public static Map<String,String> getWeekDate() {
        Map<String,String> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);

        map.put("mondayDate", weekBegin);
        map.put("sundayDate", weekEnd);
        return map;
    }

    //获取本月的第一天
    public static String getMonthFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }
    //获取本月的最后一天
    public static String getMonthLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }


}
